<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11">
<meta name="verify-v1" content="GeXKtanER98tyl4cTdtBqKhayl5odjJIjPACPNxPlNY=">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="icon" type="image/png" href="http://akrabat.com/favicon.png">
<title>Access view variables in another view model  –   Rob Allen's DevNotes</title>

<link href="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/css.css" rel="stylesheet" type="text/css">

<meta name="readability-verification" content="gVhhHPJN2LhzPExSW85N9vxBRBGz9nmUErVYVLt7">
<meta name="generator" content="WordPress 3.5.1"> <!-- leave this for stats -->

<link href="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/css.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/style.css" type="text/css" media="screen">
<link rel="alternate" type="application/rss+xml" title="Rob Allen's DevNotes RSS Feed" href="http://akrabat.com/feed/">

<style type="text/css" media="screen">


</style>

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://akrabat.com/xmlrpc.php?rsd">
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://akrabat.com/wp-includes/wlwmanifest.xml"> 
<link rel="prev" title="Returning JSON using the Accept header in ZF2" href="http://akrabat.com/zend-framework-2/returning-json-using-the-accept-header-in-zf2/">
<link rel="next" title="Getting started with Natural Load Testing" href="http://akrabat.com/review/getting-started-with-natural-load-testing/">
<meta name="generator" content="WordPress 3.5.1">
<link rel="canonical" href="http://akrabat.com/zend-framework-2/access-view-variables-in-another-view-model/">
<link rel="shortlink" href="http://akrabat.com/2253">
<link rel="payment" type="text/html" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework-2%2Faccess-view-variables-in-another-view-model%2F&amp;language=en_GB&amp;category=text&amp;title=Access+view+variables+in+another+view+model&amp;description=Developing+PHP+software+in+the+Real+World%2C+by+Rob+Allen&amp;tags=blog">

<link rel="openid.server" href="http://www.myopenid.com/server">
<link rel="openid.delegate" href="http://akrabat.myopenid.com/">

</head>
<body data-twttr-rendered="true">
<div id="page">


<div id="header">
	<div id="headerimg">
		<h1><a href="http://akrabat.com/">Rob Allen's DevNotes</a></h1>
		<div class="description">Developing PHP software in the Real World, by Rob Allen</div>
	</div>
	<ul class="topnav">
		<li><a href="http://akrabat.com/">Home</a></li>
		<li><a href="http://akrabat.com/zend-framework-2-tutorial/">ZF2 tutorial</a></li>
		<li><a href="http://akrabat.com/zend-framework-tutorial/">ZF1 tutorial</a></li>
		<li><a href="http://akrabat.com/talks/">Talks</a></li>
		<li><a href="http://akrabat.com/archives/">Archives</a></li>
		<li><a href="http://akrabat.com/about/">About</a></li>
	</ul>
</div>
<div id="container">
	<div id="content" class="column">
    <div id="pad">

  		<div class="navigation">
			<div class="alignleft">« <a href="http://akrabat.com/zend-framework-2/returning-json-using-the-accept-header-in-zf2/" rel="prev">Returning JSON using the Accept header in ZF2</a></div>
			<div class="alignright"><a href="http://akrabat.com/review/getting-started-with-natural-load-testing/" rel="next">Getting started with Natural Load Testing</a> »</div>
		</div>

		<div class="post " id="post-2253">
			<h2><a href="http://akrabat.com/zend-framework-2/access-view-variables-in-another-view-model/" rel="bookmark" title="Permanent Link: Access view variables in another view model">Access view variables in another view model</a></h2>

			<div class="entry">
				<p>Unlike Zend Framework 1, the view layer in Zend Framework 2 
separates the variables assigned to each view model. This means that 
when you are in the layout view script, you don't automatically have 
access to variables that were assigned the the action's view model and 
vice versa. </p>
<h3>Accessing action variables in the layout</h3>
<p>Consider this controller code:</p>
<p><code><br>
class IndexController extends ActionController<br>
{<br>
    public function indexAction()<br>
    {<br>
        return array('myvar' =&gt; 'test');<br>
    }<br>
}<br>
</code></p>
<p>If you are in the layout.phtml, then to retrieve this value you do:</p>
<p><strong>layout.phtml:</strong><br>
<code><br>
&lt;?php<br>
$children = $this-&gt;viewModel()-&gt;getCurrent()-&gt;getChildren();<br>
$child = $children[0];<br>
?&gt;<br>
<!-- some HTML --><br>
&lt;?php echo $this-&gt;escape($child-&gt;myvar);?&gt;<br>
</code></p>
<p>If you really want to make sure you collect the correct child view model, then you could iterate over <tt>$children</tt> and look for the child that has the correct <tt>captureTo</tt> name set. For the action's view model, this defaults to <tt>content</tt>:</p>
<p><strong>layout.phtml:</strong><br>
<code><br>
&lt;?php<br>
$children = $this-&gt;viewModel()-&gt;getCurrent()-&gt;getChildren();<br>
foreach($children as $child) {<br>
    if ($child-&gt;captureTo() == 'content') {<br>
        break;<br>
    }<br>
}<br>
?&gt;<br>
<!-- some HTML --><br>
&lt;?php echo $this-&gt;escape($child-&gt;myvar);?&gt;<br>
</code></p>
<h3>Accessing layout variables in the action view</h3>
<p>If you have assigned a variable to the layout's view model in, say, an event listener within <tt>Module.php</tt>:</p>
<p><strong>Module.php:</strong><br>
<code><br>
    public function onBootstrap($e)<br>
    {<br>
        $application = $e-&gt;getParam('application');<br>
        $viewModel = $application-&gt;getMvcEvent()-&gt;getViewModel();<br>
        $viewModel-&gt;some_config_var = '12345';<br>
    }<br>
</code></p>
<p>This is how you access <tt>some_config_var</tt> in the action view:</p>
<p><strong>view/index/index.html:</strong><br>
<code><br>
&lt;?php echo $this-&gt;escape($this-&gt;layout()-&gt;some_config_var); ?&gt;<br>
</code></p>
<p>Another, more long winded way is to use the <tt>getRoot()</tt> method on the <tt>viewModel</tt> view helper:</p>
<p><strong>view/index/index.html:</strong><br>
<code><br>
&lt;?php<br>
$layoutViewModel = $this-&gt;viewModel()-&gt;getRoot();<br>
?&gt;<br>
&lt;!-- Some HTML --&gt;<br>
&lt;?php echo $this-&gt;escape($layoutViewModel-&gt;some_config_var); ?&gt;<br>
</code></p>
<h3>Setting configuration variables into the view</h3>
<p>It therefore follows that if you need to set a variable that could be
 accessed from any view script, it's easiest to set it into the layout's
 view model and then access it via the <tt>layout()</tt> view script. 
This is handy for view layer config variables that you want to store in 
your config files, such as the Google search API key.</p>
<p><strong>Application/config/module.config.php:</strong><br>
<code><br>
&lt;?php<br>
return array(<br>
    'layout' =&gt; array(<br>
        'google_search_api_key' =&gt; '1234567890',<br>
    ),<br>
</code></p>
<p><strong>Application/Module.php:</strong><br>
<code><br>
    public function onBootstrap($e)<br>
    {<br>
        $application = $e-&gt;getParam('application');<br>
        $config = $e-&gt;getParam('config');</code></p><code>
</code><p><code>        $viewModel = $application-&gt;getMvcEvent()-&gt;getViewModel();<br>
        $viewModel-&gt;config = $config-&gt;layout;<br>
    }<br>
</code></p>
<p><strong>view/search/index.html:</strong><br>
<code><br>
&lt;?php echo $this-&gt;layout()-&gt;config-&gt;google_search_api_key; ?&gt;<br>
</code></p>

				
                <div class="social">
                    <span>Like this article? </span>
                    <ul>
                        <li><a href="http://akrabat.com/?flattrss_redirect&amp;id=2253&amp;md5=8a905fa9470e87ebdd62aafdcc8abc30" title="Flattr" target="_blank"><img src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/flattr-badge-large.png" alt="flattr this!"></a></li>
                        <li><iframe data-twttr-rendered="true" title="Twitter Tweet Button" style="width: 55px; height: 20px;" class="twitter-share-button twitter-count-none" src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/tweet_button.html" allowtransparency="true" frameborder="0" scrolling="no"></iframe><script src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/ga.js" async="" type="text/javascript"></script><script type="text/javascript" src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/widgets.js"></script></li>

                    </ul>
                </div>

				<p class="postmetadata alt">
					<small>
						This entry was posted
												on Tuesday, 3rd April 2012 at 12:15						and is filed under <a href="http://akrabat.com/category/zend-framework-2/" title="View all posts in Zend Framework 2" rel="category tag">Zend Framework 2</a>.
												
					</small>
				</p>

			</div>
		</div>

	
<!-- You can start editing here. -->


  		<!-- If comments are closed. -->
			    	    
	


	
	</div>
	</div>
	<div id="sidebar" class="column">
		<ul>

			<li>
				<form method="get" id="searchform" action="http://akrabat.com/">
<div><input name="s" id="s" type="text">
<input id="searchsubmit" value="Search" type="submit">
</div>
</form>
			</li>
                        <li style="margin-top: 10px;">
                           <a href="http://itunes.apple.com/app/daily-jotter/id411293034?mt=12" title="">
 <img src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/dj-banner-150.jpg" alt="Daily Jotter on iTunes" border="0" height="225" width="150"></a>
                        </li>
                        <li style="margin-top: 10px;">
                           <p style="margin-bottom: 3px;">My <a href="http://www.zendframeworkinaction.com/">Zend Framework Book</a>:</p>
                           <a href="http://www.zendframeworkinaction.com/" title="">
 <img src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/allen_cover150.jpg" alt="Zend Framework in Action cover" border="0" height="188" width="150"></a>
                        </li>
			<li><h2>Rob Allen</h2>
			<p>
Rob is a PHP and Zend Framework expert based in Worcester, UK. He is the Technical Director for <a href="http://www.bigroominternet.co.uk/">Big Room Internet</a> and the author of <a href="http://www.zendframeworkinaction.com/">Zend Framework in Action</a>.</p>
<p>He also supports the <a href="http://www.cahp.girl-wonder.org/">Con Anti-Harrassment Project</a>.</p>
<p>Around the web:<br>
Twitter: <a href="http://www.twitter.com/akrabat">@akrabat</a><br>
Flickr: <a href="http://www.flickr.com/photos/akrabat">photos</a><br>
Linked in: <a href="http://uk.linkedin.com/in/akrabat">profile</a><br>
</p>

			</li>

                        <li style="margin-top: 10px;">
                           
                           <a href="http://www.zend.com/en/services/certification/framework/education-advisory-board-zf" title="">
 <img src="Access%20view%20variables%20in%20another%20view%20model%20%E2%80%93%20Rob%20Allen%27s%20DevNotes_files/ZF-Education-Advisory-Board-S.png" alt="Zend Framework Education Advisory Board Member" border="0" height="35" width="155"></a>
                        </li>

			<li class="pagenav"><h2>Other pages</h2><ul><li class="page_item page-item-95"><a href="http://akrabat.com/akras-diary/">Akra's Diary</a></li>
<li class="page_item page-item-1739"><a href="http://akrabat.com/disclosure-policy/">Disclosure policy</a></li>
<li class="page_item page-item-18"><a href="http://akrabat.com/dynamic-javascript-tabs/">Dynamic JavaScript Tabs</a></li>
<li class="page_item page-item-2066"><a href="http://akrabat.com/license-for-code-published-on-this-site/">License for code on this site</a></li>
<li class="page_item page-item-459"><a href="http://akrabat.com/shorter-links/">Shorter Links WP plugin</a></li>
</ul></li>


			
		</ul>
	</div>
</div>
<div id="footer-wrapper">
<div id="footer">
	<p style="float: right">Copyright © 2005-2013 Rob Allen. All rights reserved.</p>
	<p>
		<a href="http://akrabat.com/disclosure-policy/">Disclosure policy</a>
		| <a href="http://akrabat.com/license-for-code-published-on-this-site/">License for code on this site</a>
		| <a href="feed:http://akrabat.com/feed/">Entries RSS</a>
		| <a href="feed:http://akrabat.com/comments/feed/">Comments</a>
		<!-- 26 queries. 0.190 seconds. -->
		
	</p>
</div>
</div>
</div>


<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-365662-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>







</body></html>
<!-- Dynamic page generated in 0.181 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-03 19:07:25 -->
<!-- super cache -->